using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using NorthStar.Driver.Application;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using TheNorthStar.Api.Requests;
using TheNorthStar.Api.Results;
using Timer = System.Timers.Timer;

namespace NorthStar.Driver
{
    [Activity(Label = "Current Booking")]
    public class JobingActivity : Activity
    {
        string m_driverId;
        string m_bookingId;
        private string fullAddress;
        private string customerPhoneNumber;
        private Timer checkBookingStatusTimer;

        private string GetCustomerAddress( )
        {
            return Intent.GetStringExtra( "address" ) ;
        }
       
        private void SetDriverAndCustomerStatus()
        {
            m_driverId = Intent.GetStringExtra("driverID");
            m_bookingId = Intent.GetStringExtra( "bookingID" );
            var api = new ConnectToSever(Helper.GetServer(ApplicationContext));
            DriverLogon driver = new DriverLogon();
           // Booking booking = RequestToSever();
            driver.driverID = Int32.Parse(m_driverId);
           // driver.driverStatus = status1;
            Booking booking = new Booking();
            booking.BookingId = Int32.Parse( this.m_bookingId );
            //booking.Status = status2;
            api.PickedUpBooking( driver,booking );
        }
        private void Noshow()
        {
            m_driverId = Intent.GetStringExtra("driverID");
            m_bookingId = Intent.GetStringExtra("bookingID");
            var api = new ConnectToSever(Helper.GetServer(ApplicationContext));
            DriverLogon driver = new DriverLogon();
            // Booking booking = RequestToSever();
            driver.driverID = Int32.Parse(m_driverId);
            // driver.driverStatus = status1;
            Booking booking = new Booking();
            booking.BookingId = Int32.Parse(this.m_bookingId);
            api.NoShow(driver, booking);
            Intent a = new Intent(this, typeof(MainActivity));
            a.PutExtra("driverID", m_driverId);
            a.AddFlags(ActivityFlags.ClearTop);
            StartActivity(a);   
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Jobing);
            m_driverId = Intent.GetStringExtra( "driverID" );
            m_bookingId = Intent.GetStringExtra( "bookingID" );

            fullAddress = Intent.GetStringExtra("address");
            customerPhoneNumber = Intent.GetStringExtra("customerPhoneNumber");
            TextView textview = FindViewById<TextView>(Resource.Id.GreetingAndAdress);
            Button button = FindViewById<Button>( Resource.Id.PickUpButton);
            Button noShow = FindViewById<Button>(Resource.Id.NoShowButton);
            Button btnCallCustomer = FindViewById<Button>(Resource.Id.Call);
            noShow.Click += (sender, args) =>
                {
                    Noshow();
                };
            btnCallCustomer.Click += (sender, args) =>
                                         {
                                             Intent callIntent = new Intent(Intent.ActionCall);
                                             callIntent.SetData(Android.Net.Uri.Parse("tel:" + customerPhoneNumber));
                                             StartActivity(callIntent);
                                         };

            textview.Text = fullAddress;
            button.Click += delegate 
            {
                var sending = new Intent( this, typeof( SendingActivity ) );
                sending.PutExtra( "driverID", m_driverId );
                sending.PutExtra("bookingID", m_bookingId);
                SetDriverAndCustomerStatus();
                StartActivity( sending );
            };

            checkBookingStatusTimer = new Timer(10000);
            checkBookingStatusTimer.Elapsed += (sender, args) => { ThreadPool.QueueUserWorkItem(x => CheckWork()); };
            checkBookingStatusTimer.Start();
            // Create your application here
        }

        private void CheckWork()
        {
            checkBookingStatusTimer.Stop();
            var api = new ConnectToSever(Helper.GetServer(ApplicationContext));
            int bookingId = int.Parse(m_bookingId);
            string result = api.GetBooking(bookingId);
            Booking booking = Newtonsoft.Json.JsonConvert.DeserializeObject<Booking>(result.ToString());
            
            if (booking != null && booking.Status == "CANCELLED")
            {
                
                RunOnUiThread(() =>
                {
                   
                    AlertDialog ad = new AlertDialog.Builder(this).Create();
                    ad.SetTitle("Booking Cancelled");
                    ad.SetMessage("The customer has cancelled their booking");
                    ad.SetButton("Ok", (s, e) =>
                    {
                        Intent a = new Intent(this, typeof(MainActivity));
                        a.PutExtra("driverID", m_driverId);
                        a.AddFlags(ActivityFlags.ClearTop);
                        StartActivity(a);

                    });
                    //ad.SetButton2("Reject", (s, e) => { CallForNewJob(); });
                    ad.Show();
                });


            }
            else
            {
                checkBookingStatusTimer.Start();
            }
          
        }

        protected override void OnDestroy()
        {
            checkBookingStatusTimer.Stop();
            base.OnDestroy();
        }
    }
}
